﻿using Microsoft.Extensions.Logging;
using SHKF.B2C.YouZanYun.Model.Entity;
using SqlSugar;
using System; 

namespace SHKF.B2C.YouZanYun.BLL
{
    public class TokenBLL
    {
        private readonly SqlSugarClient db;

        public TokenBLL(SqlSugarClient db,
            ILogger<TokenBLL> logger)
        {
            db.ChangeDatabase("PtE_YZY_Center");
            this.db = db;
        }

        public TokenEntity Save(TokenEntity token)
        {
            TokenEntity info = Get(token.authority_id);
            token.pDate = DateTime.Now;
            if (info == null)
            {
                token.SN = this.db
                    .Insertable<TokenEntity>(token)
                    .ExecuteReturnBigIdentity(); 

            }
            else
            {
                token.SN = info.SN; 
               int result = this.db
                    .Updateable<TokenEntity>(token) 
                    .ExecuteCommand();
            }
            return token;
        }

        public TokenEntity Get(string authority_id)
        {
            TokenEntity token = this.db
                .Queryable<TokenEntity>()
                .Where(oo=>oo.authority_id == authority_id)
                .First();
            //TokenEntity token = this.freeSql.Select<TokenEntity>()
            //       .Where(oo => oo.authority_id == authority_id)
            //       .First();

            return token;
        }
    }
}
